//
//  DYTopView.m
//  BaiSi
//
//  Created by mac on 16/10/29.
//  Copyright © 2016年 DeYi. All rights reserved.
//

#import "DYTopView.h"
#import "DYThemeItem.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "NSDate+LHDate.h"

@interface DYTopView()

@property (weak, nonatomic) IBOutlet UIImageView *iconImage;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;


@end

@implementation DYTopView

- (void)setItem:(DYThemeItem *)item {
    [super setItem:item];
    [_iconImage sd_setImageWithURL:[NSURL URLWithString:item.profile_image] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]];
    _nameLabel.text = item.name;
    _contentLabel.text = item.text;
    
    //日期处理
    NSString *timeStr = item.passtime;
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *passDate = [format dateFromString:timeStr];
    NSDateComponents *deltaTime = [passDate deltaWithNow];
    if ([passDate isThisYear]) {//今年
        if ([passDate isThisToday]) {//今天
            if (deltaTime.hour >= 1) {//1小时前
                timeStr = [NSString stringWithFormat:@"%ld小时前",deltaTime.hour];
             
            } else if (deltaTime.minute >= 1) {//一分钟前
                timeStr = [NSString stringWithFormat:@"%ld分钟前",deltaTime.minute];
                
            } else {//刚刚
                timeStr = [NSString stringWithFormat:@"刚刚"];
                
            }
            
        } else if ([passDate isThisYesterday]){//昨天
           
            format.dateFormat = @"昨天 HH:mm";
            timeStr = [format stringFromDate:passDate];
          
        } else {//2天前
            format.dateFormat = @"MM-dd HH:mm";
            timeStr = [format stringFromDate:passDate];
            
        }
        
        
    }
    _timeLabel.text = timeStr;
}


- (IBAction)ClickMore:(UIButton *)sender {
    
    UIViewController *rootVc = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"收藏" style:UIAlertActionStyleDefault handler:nil];
     UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"举报" style:UIAlertActionStyleDefault handler:nil];
     UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertVc addAction:action1];
    [alertVc addAction:action2];
    [alertVc addAction:action3];
    [rootVc presentViewController:alertVc animated:YES completion:nil];
}

@end
